package we.common;

import java.io.PrintWriter;
import java.io.StringWriter;
import we.logger.ILogController;
import we.logger.ILogController.LogMode;
import we.logger.scribe.CScribeLogController;

public class CCommonHandler {
    private static ILogController logger = new CScribeLogController();
    private CCommonHandler() {
    }
    public static boolean DEBUG = true;

    synchronized public static void trace(Object s) {
        if (DEBUG) {
            System.out.println(s);
        }
    }

    public static void writeActionLog(String ownerId, String victimId,
            String ipOrUrl, String action,
            String param1, String param2,
            String param3, String param4,
            String param5, int goldChange,
            int xuChange, int expChange) {

        String data =
                ownerId + "|" + victimId + "|" + ipOrUrl + "|" + action + "|"
                + param1 + "|" + param2 + "|" + param3 + "|" + param4 + "|"
                + param5 + "|" + goldChange + "|" + xuChange + "|" + expChange;

        new CScribeLogController().writeLog(LogMode.ACTION, data);
    }

    public static void writeErrLog(Throwable t) {
        logger.writeLog(LogMode.ERROR, getStackTrace(t));
        t.printStackTrace();
    }

    private static String getStackTrace(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }
}
